//
//  BGStatusModel.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/24.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit

class BGStatusModel: NSObject {
    // MARK: - 模型属性
    /// 创建时间
    var created_at: String?
    /// 微博ID
    var id: Int64 = 0
    /// 微博信息内容
    var text: String?
    /// 微博来源
    var source: String?
    //  用户数据
    var user: BGUserModel?
    //  转发微博
    var retweeted_status: BGStatusModel?
    //  配图
    var pic_urls: [BGHomePictureModel]?
    
    // 转发数
    var reposts_count: Int = 0
    // 评论数
    var comments_count: Int = 0
    // 表态数
    var attitudes_count: Int = 0
    
    //KVC构造函数
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    //因为user是一个模型.所以要重写KVC方法
    override func setValue(value: AnyObject?, forKey key: String) {
        //判断是否为user字段
        if key == "user" {
            
            //判断能否转为字典
            guard let dict = value as? [String: AnyObject] else {
                return
            }
            //能转就转为模型
            user = BGUserModel(dict: dict)
        }else if key == "pic_urls"{
            //图片
            guard let pics = value as? [[String: AnyObject]] else {
                return
            }
            
            //创建临时数组
            var tempArray: [BGHomePictureModel] = [BGHomePictureModel]()
            //遍历数组,字典转模型
            for dict in pics {
                let picModel = BGHomePictureModel(dict: dict)
                tempArray.append(picModel)
            }
            self.pic_urls = tempArray
            
        }else if key == "retweeted_status"{
            //转发微博
            //判断是否有数据且能转换为字典
            guard let retweet = value as? [String: AnyObject] else {
                return
            }
            //KVC
            retweeted_status = BGStatusModel(dict: retweet)
            
        }else {
            super.setValue(value, forKey: key)
        }
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    

}
